/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.menosehmais.controller.rest;

import br.com.caelum.vraptor.Delete;
import br.com.caelum.vraptor.Get;
import static br.com.caelum.vraptor.view.Results.json;

import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Put;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.Validator;
import br.com.menosehmais.business.ContratoBO;
import br.com.menosehmais.business.EmpresaBO;
import br.com.menosehmais.business.PortletBO;
import br.com.menosehmais.dto.EmpresaDTO;
import br.com.menosehmais.framework.loggin.StackTraceLogger;
import br.com.menosehmais.framework.persistence.DomainObject;
import br.com.menosehmais.framework.persistence.Id;
import br.com.menosehmais.framework.presentation.AbstractRestController;
import br.com.menosehmais.model.EmpresaDO;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Junior JAFJ
 */
@Resource
public class ManterEmpresaController extends AbstractRestController {

    private final EmpresaBO empresaBO;
    private final PortletBO portletBO;
    private final ContratoBO contratoBO;

    public ManterEmpresaController(HttpServletRequest request, Result result, 
            Validator validator, EmpresaBO empresaBO, PortletBO portletBO, ContratoBO contratoBO) {
        super(request, result, validator);
        this.empresaBO = empresaBO;
        this.portletBO = portletBO;
        this.contratoBO = contratoBO;
    }

    @Path("/manterEmpresa")
    public void indexManterEmpresa(){}

    @Post
    @Path("/rest/empresa/")
    public void create(EmpresaDTO empresaDTO) {
        validator.validate(empresaDTO);
         if(validator.hasErrors()){
            validator.onErrorForwardTo(this.<DomainObject>getRestResponseControllerClass()).error();
        }
        EmpresaDO empresaDO = empresaDTO.toDomainObject();
        DomainObject domainObject = empresaBO.insert(empresaDO);

        if (domainObject != null) {
           result.use(json()).withoutRoot().from(domainObject).serialize();
        } else {
           result.forwardTo(this.<DomainObject>getRestResponseControllerClass()).error();
        }
    }

    @Put
    @Path("/rest/empresa/{id}")
    public void update(Id id, EmpresaDTO empresaDTO) {
        empresaDTO.setId(id.toString());
       validator.validate(empresaDTO);
       if (validator.hasErrors()) {
            validator.onErrorForwardTo(this.<DomainObject>getRestResponseControllerClass()).error();
       }
       EmpresaDO empresaDO = empresaDTO.toDomainObject();
       DomainObject domainObject = empresaBO.update(empresaDO);

       if (domainObject != null) {
            result.use(json()).withoutRoot().from(domainObject).serialize();
       } else {
            result.forwardTo(this.<DomainObject>getRestResponseControllerClass()).error();
       }
    }

    @Get
    @Path("/rest/empresa/{id}")
    public void show (Id id) {
        EmpresaDO empresaDO = empresaBO.selectById(id.toString());
        if (empresaDO != null) {
            /*
             * Carregando a lista de portlets da empresa
             */
            empresaDO.setPortlet(portletBO.listarEmpresaPortlet(empresaDO));

            /*
             * Carregando a lista de contratos da empresa
             */
            empresaDO.setContratos(contratoBO.listarEmpresaContrato(empresaDO));
                                   
            result.use(json()).withoutRoot().from(empresaDO).serialize();
        } else {
            result.forwardTo(this.<DomainObject>getRestResponseControllerClass()).error();
        }        
    }

    @Delete
    @Path("/rest/empresa/{id}")
    public void destroy(Id id) {
        EmpresaDO empresaDO = empresaBO.selectById(id.toString());
        if (empresaDO != null) {
            try {
                empresaBO.delete(empresaDO);
                result.use(json()).withoutRoot().from(true).serialize();
            } catch (Exception e) {
                StackTraceLogger.INSTANCE.printStackTrace(e);
                result.forwardTo(this.<EmpresaDO>getRestResponseControllerClass()).error();
            }
        } else {
            result.forwardTo(this.<EmpresaDO>getRestResponseControllerClass()).error();
        }
    }
}
